import baseRequest from '@/utils/lz-request'
import { getToken } from '@/utils/localStorage'

const request = (url, value = {}, method = 'post', options = {}) => {
  const token = getToken()
  if (method === 'post') {
    return baseRequest(url, {
      method: 'post',
      data: value,
      headers: {
        token
      }
    })
  }
  if (method === 'get') {
    return baseRequest(url, { params: value, headers: { token } }, options)
  }
  if (method === 'formdata') {
    return baseRequest({
      method: 'post',
      url,
      data: value,
      transformRequest: [
        data => {
          let ret = ''
          for (const it in data) {
            ret += `${encodeURIComponent(it)}=${encodeURIComponent(data[it])}&`
          }
          ret = ret.substring(0, ret.length - 1)
          return ret
        }
      ],
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token
      }
    })
  }
}

export default request
